package com.yonyou.iuap.corp.demo.yonbip.singlesignon;

import com.yonyou.iuap.corp.demo.crypto.SignHelper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 第三方系统通过页面点击的方案获取用友数据信息---模拟登录  ---加入了权限的概念
 * 例如，app通过单点登录的方式获取到用友的用户信息等等   --适用于app客户端，用友作为服务端的情况
 * @author: nishch
 * @create: 2021-03-12
 **/
public class ThirdGetInfoByUserToken extends SingleSignOn{
    //TODO:注意事项,前置操作
    //1、请查看单点登录指导文档：https://csdoc.yonyoucloud.com/mybook/single-login/1-/single-login.html
    //1、在应用构建新增一个页面，目前获取serviceCode,单点登录到指定节点必须条件,并发布
    //2、我的应用找到应用构建自动生成的应用、服务列表编辑:  web端主页，填写登录成功过渡页面地址；web端打开方式：在新页面打开；集成免登策略：友空间
    //登录成功过渡页
    String loginSuccess = "https://www.baidu.com";

    @Value("${app.key}")
    private String appKey;

    @Value("${app.secret}")
    private String appSecret;

    /**
     * 第一步从重定向地址栏获取免登code
     * @throws Exception
     */
    @Test
    public void getYKJCode() throws Exception {
        //ThirdUserEntity entity = new ThirdUserEntity("nhbfpxn9","12345623","nscer123","nscer123","v7hr2yho","15fd3a1e-7e56-4678-96a2-30343080db87",null,null);
        ThirdUserEntity entity = new ThirdUserEntity("nhbfpxn9","123456","nscer","nscer","v7hr2yho","15fd3a1e-7e56-4678-96a2-30343080db87",null,null);
        String useUrl = getSingleSignOnUrlToNode(entity);
        oepnEexplorer(useUrl);
    }

    /**
     * 第二步根据免登code和租户访问令牌获取用户访问令牌--userToken
     * 可以根据用户访问令牌调用开放平台的接口，获取的是当前用户权限过滤后的数据信息
     * refresh_token是24个小时，userToken是两个小时，在refresh有效期内可以刷userToken
     * @throws Exception
     * @Reponse
     * {
     * 	"code": "00000",
     * 	"message": "成功！",
     * 	"data": {
     * 		"user_access_token": "y2CnnR9aWM",
     * 		"token_type": "Bearer",
     * 		"refresh_token": "EWkIx8CPhq",
     * 		"expires_in": 7200,
     * 		"id_token": "eyJhbGciOiJIUzI1NiJ9.eyJ5aHRVc2VySWQiOiJ1c3BhY2VfOTc0NTA3IiwidGVuYW50SWQiOiJ2N2hyMnlobyIsInVzZXJOYW1lIjoi5p2O5YWL6aOeIiwibW9iaWxlIjoiMTM2NzExMTc0NTEiLCJlbWFpbCI6Imxpa2ZrQHlvbnlvdS5jb20iLCJleHAiOjE2MTU1MjgxODZ9.5jIKeiEnA6zG9Y4CgYNLhPbEu0UfILede8BS646fP0M"
     *        }
     * }
     */
    @Test
    public void getUserToken() throws Exception {
        //TODO:从第一步地址栏获取code
        String code = "933bda0bcd34879295d7160b26cd06ccb899c211ca7af77bf1815f9313ad";
        String url = "https://api.diwork.com/open-auth/selfAppAuth/getUserToken";
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("code",code);
        baseApi.doGet(url,params);
    }

    /**
     * 第三步获调用业务接口 ----会发现进行了权限过滤  采购入库单
     * TODO:从第二步获取的user_access_token
     * @throws Exception
     */
    @Test
    public void getUnit() throws Exception {
        //TODO:
        String user_access_token = "jYjAqOwLJH";
        String url = "https://api.diwork.com/yonbip/scm/purinrecord/list";
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("user_access_token",user_access_token);
        params.put("pageIndex",1);
        params.put("pageSize",10);
        baseApi.doPostByUserToken(url,params);
    }

    /**
     * 根据第二步获取的refresh_token获取新的user_access_token
     * 需要注意一点：使用refreshToken获取到新的userToken后，如果旧userToken的过期时间>5分钟，则会被更新为5分钟；如果过期时间<5分钟，则过期时间不变。
     * {
     * 	"code": "00000",
     * 	"message": "成功！",
     * 	"data": {
     * 		"expired": 7200,
     * 		"user_access_token": "x2ORDULxAo"
     *        }
     * }
     * @throws Exception
     */
    @Test
    public void refreshUserToken() throws Exception {
        //TODO:根据第二步获取的refresh_token
        String refreshToken = "VmYAHAdJrn";
        String url = "https://api.diwork.com/open-auth/userToken/refresh";
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appKey", appKey);
        params.put("refreshToken",refreshToken);
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        String signature = SignHelper.sign(params, appSecret);
        params.put("signature", signature);
        baseApi.doGet(url,params);
    }

}
